﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.Utilities;
using CuaHangMayTinh.DataProvider;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class ChiTietBanHangDA
    {
        public ChiTietBanHangDA()
        {

        }

        /// <summary>
        /// Hàm thêm 1 chi tiết bán hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(ChiTietBanHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaHDBan", entity.MaHDBan);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("GiaBan", entity.GiaBan);
                return (int)DBFactory.Database.ExecuteNonQuery("ChiTietBanHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm Thêm 1 chi tiết bán hàng sử dụng transaction
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int Insert(ChiTietBanHangEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaHDBan", entity.MaHDBan);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("GiaBan", entity.GiaBan);
                return (int)DBFactory.Database.ExecuteNonQuery("ChiTietBanHang_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm update 1 chi tiết bán hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate_ID(ChiTietBanHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("MaChiTietBanHang", entity.MaChiTietBanHang);
                pb.AddParameter("MaHDBan", entity.MaHDBan);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("GiaBan", entity.GiaBan);
                int numRowEffected = DBFactory.Database.ExecuteNonQuery("ChiTietBanHang_UpDate_ID", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Get toàn bộ thông tin về chi tiết bán bằng mã bán hàng
        /// </summary>
        /// <param name="MaBanHang"></param>
        /// <returns></returns>
        public DataTable GetInfo_ByMaBanHang(int MaBanHang)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaBanHang", MaBanHang);

                return DBFactory.Database.FillDataTable("ChiTietBanHang_GetInfo_ByMaBanHang", pd.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm lấy toàn bộ thông tin về chi tiết bán hàng
        /// </summary>
        /// <returns></returns>
        public DataTable GetAll()
        {
            try
            {
                return DBFactory.Database.FillDataTable("SELECT * FROM ChiTietBanHang");
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm chuyển 1 DataTable thành 1 List ChiTietBanHangEntity
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<ChiTietBanHangEntity> ConvertToList(DataTable dt)
        {
            List<ChiTietBanHangEntity> list = new List<ChiTietBanHangEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int machitiet = (int)dt.Rows[i][0];
                int mabanhang = (int)dt.Rows[i][1];
                int mamodel = (int)dt.Rows[i][2];
                int soluong = (int)dt.Rows[i][3];
                decimal giaban = (decimal)dt.Rows[i][4];

                ChiTietBanHangEntity a = new ChiTietBanHangEntity(machitiet, mabanhang, mamodel, soluong, giaban);
                list.Add(a);
            }
            return list;
        }

    }
}
